<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Comment_model extends MY_Model
{
    private $table = 'comment';
	public function __construct()
	{
		parent::__construct($this->table,['file_model']);
	
	}

    public function getCompany($where)
    {
        $data = $where;
        if ($this->input->get('has_image', TRUE) === "1") {
            $data['comment.has_image'] = 1;
        } else if($this->input->get('has_image', TRUE) === "0") {
            $data['comment.has_image'] = 0;
        }
        if ($this->input->get('trade_no', TRUE)) {
            $data['orderlist.trade_no'] = $this->input->get('trade_no', TRUE);
        }
        if ($this->input->get('realName', TRUE)) {
            $this->db->like('user.realName', $this->input->get('realName', TRUE));
        }
        $this->_join('company','company.companyId=comment.listId', 'left');
        $this->_join('user','user.userId=comment.userId', 'left');
        $this->_join('orderlist','orderlist.orderId=comment.orderId', 'left');
        $this->_get_db($data);

        $this->db->select('COUNT(*) as count');
        $info = $this->db->get('comment')->row_array();

        $data['limit'] = $this->input->get('limit', TRUE);
        $data['page'] = $this->input->get('page', TRUE);
        if ($this->input->get('realName', TRUE)) {
            $this->db->like('user.realName', $this->input->get('realName', TRUE));
        }
        $this->db->order_by('comment.createTime', 'DESC');
        $this->db->select('user.userId,user.avatar,user.nickName,user.realName,orderlist.trade_no,
                comment.commentId,comment.judge,comment.content,comment.createTime,
                comment.zanNum, comment.replyNum');
        $this->_get_db($data);
        $info['data'] = $this->db->get('comment')->result_array();

        foreach ($info['data'] as &$value) {
            if ($value['createTime'] != '0') {
                $value['createTime'] = date('Y-m-d  H:i:s', $value['createTime']);
            }
        }
        $infos = $this->Images($info);
        return $infos;
    }

    public function getProject($where)
    {
        $data = $where;
        if ($this->input->get('has_image', TRUE) === "1") {
            $data['comment.has_image'] = 1;
        } else if($this->input->get('has_image', TRUE) === "0") {
            $data['comment.has_image'] = 0;
        }
        if ($this->input->get('trade_no', TRUE)) {
            $data['orderlist.trade_no'] = $this->input->get('trade_no', TRUE);
        }
        if ($this->input->get('realName', TRUE)) {
            $this->db->like('user.realName', $this->input->get('realName', TRUE));
        }
        $this->_join('project','project.projectId=comment.listId', 'left');
        $this->_join('user','user.userId=comment.userId', 'left');
        $this->_join('orderlist','orderlist.orderId=comment.orderId', 'left');
        $this->_get_db($data);

        $this->db->select('COUNT(*) as count');
        $info = $this->db->get('comment')->row_array();

        $data['limit'] = $this->input->get('limit', TRUE);
        $data['page'] = $this->input->get('page', TRUE);
        if ($this->input->get('realName', TRUE)) {
            $this->db->like('user.realName', $this->input->get('realName', TRUE));
        }
        $this->db->order_by('comment.createTime', 'DESC');
        $this->db->select('user.userId,user.avatar,user.nickName,user.realName,project.title,orderlist.trade_no,
                comment.commentId,comment.judge,comment.content,comment.createTime,comment.zanNum, comment.replyNum');
        $this->_get_db($data);
        $info['data'] = $this->db->get('comment')->result_array();

        foreach ($info['data'] as &$value) {
            if ($value['createTime'] != '0') {
                $value['createTime'] = date('Y-m-d  H:i:s', $value['createTime']);
            }
        }
        $infos = $this->Images($info);
        return $infos;
    }
    public function getTechnician($where)
    {
        $data = $where;
        if ($this->input->get('has_image', TRUE) === "1") {
            $data['comment.has_image'] = 1;
        } else if($this->input->get('has_image', TRUE) === "0") {
            $data['comment.has_image'] = 0;
        }
        if ($this->input->get('trade_no', TRUE)) {
            $data['orderlist.trade_no'] = $this->input->get('trade_no', TRUE);
        }
        if ($this->input->get('realName', TRUE)) {
            $this->db->like('user.realName', $this->input->get('realName', TRUE));
        }
        $this->_join('technician','technician.technicianId=comment.listId', 'left');
        $this->_join('user','user.userId=comment.userId', 'left');
        $this->_join('orderlist','orderlist.orderId=comment.orderId', 'left');
        $this->_get_db($data);

        $this->db->select('COUNT(*) as count');
        $info = $this->db->get('comment')->row_array();

        $data['limit'] = $this->input->get('limit', TRUE);
        $data['page'] = $this->input->get('page', TRUE);
        if ($this->input->get('realName', TRUE)) {
            $this->db->like('user.realName', $this->input->get('realName', TRUE));
        }
        $this->db->order_by('comment.createTime', 'DESC');
        $this->db->select('user.userId,user.avatar,user.nickName,user.realName,orderlist.trade_no,
                comment.commentId,comment.judge,comment.content,comment.createTime,comment.zanNum,
                comment.replyNum, technician.sevriceNo, technician.realName as technicianName');
        $this->_get_db($data);
        $info['data'] = $this->db->get('comment')->result_array();

        foreach ($info['data'] as &$value) {
            if ($value['createTime'] != '0') {
                $value['createTime'] = date('Y-m-d  H:i:s', $value['createTime']);
            }
        }
        $infos = $this->Images($info);
        return $infos;
    }

    public function getOne($commentId)
    {
        $data['comment.commentId'] = $commentId;
        $this->_join('user','user.userId=comment.userId', 'left');
        $this->_join('orderlist','orderlist.orderId=comment.orderId', 'left');
        $this->db->select('user.userId,user.avatar,user.nickName,user.realName,orderlist.trade_no,
                comment.commentId,comment.judge,comment.content,comment.createTime,
                comment.zanNum, comment.replyNum, comment.listId, comment.has_image');
        $this->_get_db($data);
        $info = $this->db->get('comment')->row_array();
        if ($info['createTime']) {
            $info['createTime'] = date('Y-m-d  H:i:s', $info['createTime']);
        }
        if($info['has_image']) {
            $this->db->where(['listId'=>$info['commentId'],'typeFlag'=>1]);
            $this->db->select('concat("'.IMAGEHOST.'", url) as url');
            $images=$this->db->get('file')->result_array();
            $info['images']=$images;     //添加图片数据至评价
            unset($images);
        }else {
            $info['images']=[];
        }
        return $this->getrepaly($info);
    }
    //添加图片数据至评价
    private function Images($comments){
        if($comments['data']){
            foreach($comments['data'] as $key=>$val){
                $this->db->where(['listId'=>$val['commentId'],'typeFlag'=>1]);
                $this->db->select('concat("'.IMAGEHOST.'", url) as url');
                $images=$this->db->get('file')->result_array();
                $comments['data'][$key]['images']=$images;     //添加图片数据至评价
                unset($images);
            }
        }

        return $comments;
    }

    public function getrepaly($info)
    {
        if($info){
            $this->db->join('user','user.userId=comment.userId', 'left');
            $this->db->join('orderlist','orderlist.orderId=comment.orderId', 'left');
            $this->db->where(['tocommentId'=>$info['commentId'],'replyFlag'=>1, 'deleteFlag'=>0]);
            $this->db->select('user.userId,user.avatar,user.nickName,user.realName,orderlist.trade_no,
            comment.commentId,comment.judge,comment.content,comment.createTime,
            comment.zanNum, comment.replyNum, comment.has_image');
            $repalys=$this->db->get('comment')->result_array();
            $repaly =$this->addImages($repalys);
            $info['repaly']=$repaly;     //添加图片数据至评价
            unset($repalys);
            unset($repaly);
        }
        return $info;
    }

    public function del($id)
    {
        $this->db->where('commentId', $id);
        $this->db->set('deleteFlag', 1);
        $this->db->update('comment');
        return $this->db->affected_rows();
    }

    public function addrepaly($companyId)
    {
        $data['userId']=$this->input->post('userId', TRUE);
        $data['content']=$this->input->post('content', TRUE);
        $data['tocommentId']=$this->input->post('tocommentId', TRUE);
        $imgeas=$this->input->post('imagePath', TRUE);
        $this->db->trans_begin();

        if(isset($data['tocommentId'])) {
            $info = $this->getOne($data['tocommentId']);
        }
        if(!empty($imgeas)) {
            $data['has_image']=1;
        }
        $data['typeFlag']=1;
//        $data['companyId']= $companyId;
        $data['replyFlag']= 1;
        $data['createTime']= time();

        $this->db->insert('comment',$data);
        $id = $this->db->insert_id();
        if($id){
            $this->db->where('commentId', $data['tocommentId']);
            $this->db->set('replyNum', 'replyNum+1', FALSE);
            $this->db->update('comment');
        }
        if(!empty($imgeas)){
            $data['typeFlag']=1;
            foreach ($imgeas as $key=>$value) {
                $img['url'] = $value;
                $img['listId'] = $id;
                $img['typeFlag'] = 1;
                $img['createTime'] = time();
                $this->db->insert('file', $img);
            }
        }
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            return 0;
        }
        else
        {
            $this->db->trans_commit();
            return 1;
        }

    }
	//获取平均评价分值
	public function getAvgJudge($where){
	    $this->db->select_avg('judge');
	    $this->db->where($where);
	    $res=$this->db->get($this->table)->row_array();
	    if($res){
	        return number_format(@$res['judge'],1);        //格式化数字,保留一位小数
	    }else{
	        return 0;
	    }
	}
	//获取店铺/项目/技师的评论列表
    public function getComments($where,$dbArray){
        $select='user.userId,user.avatar,user.nickName,user.realName,
                comment.commentId,comment.judge,comment.content,comment.createTime';
        $this->db->join('project','project.projectId=comment.listId');
        $this->db->join('user','user.userId=comment.userId');
        $comments=$this->getWhere($where,$select,$dbArray);
        return $this->addImages($comments);
    }
    //添加图片数据至评价
	private function addImages($comments){
	    if($comments){
	        foreach($comments as $key=>$val){
	            $comments[$key]['createTime']=format_time($val['createTime']);     //格式化创建时间
                if(@$val['has_image'] == 1){
                    $this->db->where(['listId'=>$val['commentId'],'typeFlag'=>1]);
                    $this->db->select('concat("'.IMAGEHOST.'", url) as url');
                    $images=$this->db->get('file')->result_array();
                    $comments[$key]['images'] = $images;
                    unset($images);
                }else {
                    $comments[$key]['images'] = [];
                }
	        }
	    }
	    return $comments;
	}

    //获取订单中对公司的评价
    public function getComment_company($orderId,$userId){
        $where=['comment.orderId'=>$orderId,'comment.userId'=>$userId,'comment.typeFlag'=>0];   //0店铺,1项目,2技师
        $select='company.name,concat("'.IMAGEHOST.'",company.logo) as logo,comment.judge,comment.content';
        $this->db->join('company','company.companyId=comment.listId');
        return $this->getWhereRow($where,$select);
    }
    
    //获取订单中对项目的评价
    public function getComment_project($orderId,$userId){
        $where=['comment.orderId'=>$orderId,'comment.userId'=>$userId,'comment.typeFlag'=>1];   //0店铺,1项目,2技师
        $select='project.title,concat("'.IMAGEHOST.'",project.poster) as poster,comment.judge,comment.content';
        $this->db->join('project','project.projectId=comment.listId');
        return $this->getWhereRow($where,$select);
    }
    
    //获取订单中对技师的评价
    public function getComment_technician($orderId,$userId){
        $where=['comment.orderId'=>$orderId,'comment.userId'=>$userId,'comment.typeFlag'=>2];   //0店铺,1项目,2技师
        $select='technician.realName,concat("'.IMAGEHOST.'",technician.avatar) as avatar,technician.sevriceNo,technician.tags,comment.judge,comment.content';
        $this->db->join('technician','technician.technicianId=comment.listId');
        return $this->getWhereRow($where,$select);
    }
    
    
    
    
    
    
    
    
    
}
?>